package com.resoft.bigdata.p2p.record;

import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.hbase.util.Bytes;

import com.resoft.bigdata.util.DateUtil;

public class MonthlyStatistics extends AbsRecord  implements RecordInterface{
	public static final String TABLE_NAME = "STA_M";
	public static final String[] FAMILY_NAMES= {"F1"};
	
	public static final byte[] DKCJE=Bytes.toBytes("DKCJE");
	public static final byte[] LVSP=Bytes.toBytes("LVSP");
	public static final byte[] RJJDJE=Bytes.toBytes("RJJDJE");
	public static final byte[] PJJKQX=Bytes.toBytes("PJJKQX");
	public static final byte[] ZJJLR=Bytes.toBytes("ZJJLR");
	public static final byte[] JKBS=Bytes.toBytes("JKBS");
	public static final byte[] DHYE=Bytes.toBytes("DHYE");
	public static final byte[] TZRS=Bytes.toBytes("TZRS");
	public static final byte[] DKJKRS=Bytes.toBytes("DKJKRS");
	
	private int yearAndMonth; //year*100+month;
	private String dateString;  //yyyy-MM-dd
	private String p2pOrgCode;
	private String p2pOrgName;
	
	private float creditBalancer; //贷款成交额（万元）--- LoanContract.xmje
	private float interestRate; //利率水平 ---- LoanContract.jklv	借款利率
	private float tmpTotalInterest; //利息----- LoanContract.xmje*LoanContract.jklv
	
	private float perCreditBalance; //人均借款金额 （万元）creditBalancer/debtors
	
	private float perLoanPeriod;   //平均借款期限（月）---- 期限类型 LoanContract.qxlx  项目期限 LoanContract.xmqx
	private float tmpTotalPerLoanPeriod;
	
	private float netInflows;//	资金净流入（万元）--- InvestInfo.tzje
	private float tmpTotaltzje; //投资金额--- InvestInfo.tzje
	private int totalCreditNumber; //	借款标数（个）---- LoanContract.xmht
	private float remainder; //	待还余额（万元）----DebtorInfo.zptyqzjr
	private int investors; //	投资人数（人）投资人ID InvestInfo.tzrid
	private int debtors; //	贷款借款人数（人）----DebtorInfo.jkr
	
	
	public MonthlyStatistics(){
		creditBalancer = 0;
		tmpTotalInterest = 0;
		tmpTotalPerLoanPeriod =0;
		totalCreditNumber = 0;
		remainder=0;
		investors=0;
		debtors=0;
	}
	
	public MonthlyStatistics addCreditBalancer(float creditBalance){
		this.creditBalancer += creditBalance;
		return this;
	}
	
	public MonthlyStatistics addTmpTotalInterest(float interest){
		this.tmpTotalInterest += interest;
		return this;
	}
	
	public MonthlyStatistics addTotalCreditNumber(int creditNumber){
		this.totalCreditNumber += creditNumber;
		return this;
	}
	
	public MonthlyStatistics addRemainder(float remainder){
		this.remainder += remainder;
		return this;
	}

	public MonthlyStatistics addInvestors(int investors){
		this.investors += investors;
		return this;
	}

	public MonthlyStatistics addDebtors(int debtors){
		this.debtors += debtors;
		return this;
	}
	
	public MonthlyStatistics addTmpTotalPerloanPeriod(float perLoanPeriod){
		this.tmpTotalPerLoanPeriod += perLoanPeriod;
		return this;
	}
	
	public MonthlyStatistics addTmpTotaltzje(float tzje){
		this.tmpTotaltzje += tzje;
		return this;
	}

	
	public int getYearAndMonth() {
		return yearAndMonth;
	}
	public void setYearAndMonth(int yearAndMonth) {
		this.yearAndMonth = yearAndMonth;
	}
	public String getDateString() {
		return dateString;
	}
	public void setDateString(String dateString) {
		this.dateString = dateString;
	}
	public String getP2pOrgCode() {
		return p2pOrgCode;
	}
	public void setP2pOrgCode(String p2pOrgCode) {
		this.p2pOrgCode = p2pOrgCode;
	}
	public String getP2pOrgName() {
		return p2pOrgName;
	}
	public void setP2pOrgName(String p2pOrgName) {
		this.p2pOrgName = p2pOrgName;
	}
	public float getCreditBalancer() {
		return creditBalancer;
	}
	public void setCreditBalancer(float creditBalancer) {
		this.creditBalancer = creditBalancer;
	}
	public float getInterestRate() {
		return interestRate;
	}
	public void setInterestRate(float interestRate) {
		this.interestRate = interestRate;
	}
	public float getPerCreditBalance() {
		return perCreditBalance;
	}
	public void setPerCreditBalance(float perCreditBalance) {
		this.perCreditBalance = perCreditBalance;
	}
	public float getPerLoanPeriod() {
		return perLoanPeriod;
	}
	public void setPerLoanPeriod(float perLoanPeriod) {
		this.perLoanPeriod = perLoanPeriod;
	}
	public float getNetInflows() {
		return netInflows;
	}
	public void setNetInflows(float netInflows) {
		this.netInflows = netInflows;
	}
	public int getTotalCreditNumber() {
		return totalCreditNumber;
	}
	public void setTotalCreditNumber(int totalCreditNumber) {
		this.totalCreditNumber = totalCreditNumber;
	}
	public float getRemainder() {
		return remainder;
	}
	public void setRemainder(float remainder) {
		this.remainder = remainder;
	}
	public int getInvestors() {
		return investors;
	}
	public void setInvestors(int investors) {
		this.investors = investors;
	}
	public int getDebtors() {
		return debtors;
	}
	public void setDebtors(int debtors) {
		this.debtors = debtors;
	}
	
	private int getDays(){
		int days = DateUtil.convert2Day(dateString, DateUtil.DATE_FORMAT);
		return Integer.MAX_VALUE-days;
	}
	
	public byte[] getRowKey() throws Exception {
		byte [] buffer = new byte[8];
		Bytes.putInt(buffer, 0, p2pOrgCode.hashCode());
		int days = getDays();
		if(days == Integer.MAX_VALUE){
			throw  new Exception("Bad date");
		}

		Bytes.putInt(buffer, 4, days);
		return buffer ; 
	}

	public Map<byte[], byte[]> getCells() {
		Map<byte[], byte[]> cells = new HashMap<byte[], byte[]>();
		Map<byte[], byte[]> superCells = super.getCells();
		cells.putAll(superCells);

		cells.put(DKCJE, Bytes.toBytes(getDKCJE()));
		cells.put(LVSP, Bytes.toBytes(getLVSP()));
		cells.put(RJJDJE, Bytes.toBytes(getPJJKQX()));
		cells.put(ZJJLR, Bytes.toBytes(getZJJLR()));
		cells.put(JKBS, Bytes.toBytes(getJKBS()));
		cells.put(DHYE, Bytes.toBytes(getDHYE()));
		cells.put(TZRS, Bytes.toBytes(getTZRS()));
		cells.put(DKJKRS, Bytes.toBytes(getDKJKRS()));
		
		return cells;
	}
	public String getCreatePhoenixTableSQL() {
		// TODO Auto-generated method stub
		return null;
	}
//	public void setValue(byte[] key, byte[] value) {
//		// TODO Auto-generated method stub
//		
//	}
	
	public String getTableName() {
		return TABLE_NAME;
	}

	public String[] getFamilies() {
		return FAMILY_NAMES;
	}

	public float getTmpTotalInterest() {
		return tmpTotalInterest;
	}

	public void setTmpTotalInterest(float tmpTotalInterest) {
		this.tmpTotalInterest = tmpTotalInterest;
	}

	public float getTmpTotalPerLoanPeriod() {
		return tmpTotalPerLoanPeriod;
	}

	public void setTmpTotalPerLoanPeriod(float tmpTotalPerLoanPeriod) {
		this.tmpTotalPerLoanPeriod = tmpTotalPerLoanPeriod;
	}

	@Override
	public String toString() {
		return "MonthlyStatistics [p2pOrgCode=" + p2pOrgCode
				+ ", creditBalancer=" + creditBalancer + ", interestRate="
				+ interestRate + ", perCreditBalance=" + perCreditBalance
				+ ", perLoanPeriod=" + perLoanPeriod + ", netInflows="
				+ netInflows + ", totalCreditNumber=" + totalCreditNumber
				+ ", remainder=" + remainder + ", investors=" + investors
				+ ", debtors=" + debtors + "]";
	}

	public float getTmpTotaltzje() {
		return tmpTotaltzje;
	}

	public void setTmpTotaltzje(float tmpTotaltzje) {
		this.tmpTotaltzje = tmpTotaltzje;
	}


	

	public String getDKCJE() {
		if(creditBalancer > 0){
			return Float.toString(creditBalancer);
		}else{
			return Float.toString(0);
		}
	}
	
	public void setDKCJE(String dKCJE) {
		try{
			float value = Float.parseFloat(dKCJE);
			creditBalancer = value;
		}catch(Exception e){
			
		}
	}

//		private float interestRate; //利率水平 ---- LoanContract.jklv	借款利率		
	public String getLVSP() {
		if(interestRate > 0){
			return Float.toString(interestRate);
		}else{
			return Float.toString(0);
		}
	}
	
	public void setLVSP(String lVSP) {
		try{
			float value = Float.parseFloat(lVSP);
			interestRate = value;
		}catch(Exception e){
			
		}
	}
	
//		private float perCreditBalance; //人均借款金额 （万元）creditBalancer/debtors
	public String getRJJDJE() {
		if(perCreditBalance > 0){
			return Float.toString(perCreditBalance);
		}else{
			return Float.toString(0);
		}
	}
	public void setRJJDJE(String rJJDJE) {
		try{
			float value = Float.parseFloat(rJJDJE);
			perCreditBalance = value;
		}catch(Exception e){
			
		}
	}
	
//		private float perLoanPeriod;   //平均借款期限（月）---- 期限类型 LoanContract.qxlx  项目期限 LoanContract.xmqx
	public String getPJJKQX() {
		if(perLoanPeriod > 0){
			return Float.toString(perLoanPeriod);
		}else{
			return Float.toString(0);
		}
	}
	public void setPJJKQX(String pJJKQX) {
		try{
			float value = Float.parseFloat(pJJKQX);
			perLoanPeriod = value;
		}catch(Exception e){
			
		}
	}

	//		private float netInflows;//	资金净流入（万元）--- InvestInfo.tzje
	public String getZJJLR() {
		if(netInflows > 0){
			return Float.toString(netInflows);
		}else{
			return Float.toString(0);
		}
	}
	public void setZJJLR(String zJJLR) {
		try{
			float value = Float.parseFloat(zJJLR);
			netInflows = value;
		}catch(Exception e){
			
		}
	}
	
//		private int totalCreditNumber; //	借款标数（个）---- LoanContract.xmht
	public String getJKBS() {
		if(totalCreditNumber > 0){
			return Integer.toString(totalCreditNumber);
		}else{
			return Integer.toString(0);
		}
	}
	public void setJKBS(String jKBS) {
		try{
			int value = Integer.parseInt(jKBS);
			totalCreditNumber = value;
		}catch(Exception e){
			
		}
	}

	//		private float remainder; //	待还余额（万元）----DebtorInfo.zptyqzjr		
	public String getDHYE() {
		if(remainder > 0){
			return Float.toString(remainder);
		}else{
			return Float.toString(0);
		}
	}
	
	public void setDHYE(String dHYE) {
		try{
			float value = Float.parseFloat(dHYE);
			remainder = value;
		}catch(Exception e){
			
		}
	}
	
//		private int investors; //	投资人数（人）投资人ID InvestInfo.tzrid
	public String getTZRS() {
		if(investors > 0){
			return Integer.toString(investors);
		}else{
			return Integer.toString(0);
		}
	}
	public void setTZRS(String tZRS) {
		try{
			int value = Integer.parseInt(tZRS);
			investors = value;
		}catch(Exception e){
			
		}
	}
	
//		private int debtors; //	贷款借款人数（人）----DebtorInfo.jkr
	public String getDKJKRS() {
		if(debtors > 0){
			return Integer.toString(debtors);
		}else{
			return Integer.toString(0);
		}
	}
	public void setDKJKRS(String dKJKRS) {
		try{
			int value = Integer.parseInt(dKJKRS);
			debtors = value;
		}catch(Exception e){
			
		}
	}
		
		

	
	
}
